<?php


require "database.php";


$base=new Db;


$array=$base->doSql('select * from ey_arctype where status=1 and lang="cn"');



function getTree($array, $pid =0, $level = 0){
    //声明静态数组,避免递归调用时,多次声明导致数组覆盖
    static $list = [];
    foreach ($array as $key => $value){
      //第一次遍历,找到父节点为根节点的节点 也就是pid=0的节点
      if ($value['parent_id'] == $pid){
        //父节点为根节点的节点,级别为0，也就是第一级
        $value['level'] = $level;
        //把数组放到list中
        $list[] = $value;
        //把这个节点从数组中移除,减少后续递归消耗
        unset($array[$key]);
        //开始递归,查找父ID为该节点ID的节点,级别则为原级别+1
        getTree($array, $value['id'], $level+1);
      }
    }
    return $list;
}
/*
* 获得递归完的数据,遍历生成分类
*/

$dd=[];

$array = getTree($array);
foreach($array as $key=> $value){
    
    
    $dd[$key]["id"]=$array[$key]["id"];
    $dd[$key]["pId"]=$array[$key]["parent_id"];
    $dd[$key]["name"]=$array[$key]["typename"];
    
    $dd[$key]["url"]='/cate_view.html?id='.$array[$key]["id"];
    $dd[$key]["target"]='content_body';
    if($array[$key]["level"]==0){
        $dd[$key]["isParent"]=true;
    }else{
        $dd[$key]["isParent"]=false;
    }
}

return json($dd);



